<?php

namespace app\common\cachemodel\acquired_astro;

use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class ZhunleappAstroExercise extends BaseCache
{
    protected $table = 'haozhungpt_ai.zhunleapp_astro_exercise';

    protected $cacheModel = 'acquired_astro.exercise_question';

    /**
     * 获取 题目分类信息
     * @return array
     */
    public static function getQuestionCategoryData()
    {
        return [
            1 => '综合题库',
            2 => '恋爱题库',
            3 => '财富题库',
            4 => '工作题库',
            5 => '人际题库',
        ];
    }

    /**
     * 开启了缓存
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->whereIn('id', $ids);
            } else
            {
                $this->where('t_id', $type[0]);
            }

            $questionResult = $this->where('status', 1)
                ->order('id ' . $sortType)
                ->column('title,status,t_id,title_img,option_a,option_b,type,a_type,b_type,extend,create_time,update_time', 'id');
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return [];
        }

        return $questionResult;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'status', 't_id', 'type', 'a_type', 'b_type', 'create_time', 'update_time']);
        }
        return $data;
    }

    public function getQuestionIds(int $tid)
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $tid)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($tid)
            {
                try
                {
                    $data = $this->where(['t_id' => $tid, 'status' => 1])->column('id');
                    $resData = [];
                    foreach ($data as $id)
                    {
                        $resData[$id] = ['id' => $id];
                    }
                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return array_keys($resData['data']);
    }

    /**
     * 获取 初始化 后天星座测试 答题数据
     * @param int $type 1=>vip体验版 2=>完整版
     * @return array
     */
    public function getInitTestQuestionData(int $type)
    {
        $categoryData1 = $this->getQuestionIds(1);
        shuffle($categoryData1);
        $num1 = count($categoryData1);
        $num2 = 0;
        $num3 = 0;
        $num4 = 0;
        $num5 = 0;
        if ($type === 2)
        {
            $categoryData2 = $this->getQuestionIds(2);
            shuffle($categoryData2);
            $num2 = count($categoryData2);
            $categoryData3 = $this->getQuestionIds(3);
            shuffle($categoryData3);
            $num3 = count($categoryData3);
            $categoryData4 = $this->getQuestionIds(4);
            shuffle($categoryData4);
            $num4 = count($categoryData4);
            $categoryData5 = $this->getQuestionIds(5);
            shuffle($categoryData5);
            $num5 = count($categoryData5);
        }
        return [
            'q1' => $categoryData1,
            'q2' => $categoryData2 ?? [],
            'q3' => $categoryData3 ?? [],
            'q4' => $categoryData4 ?? [],
            'q5' => $categoryData5 ?? [],
            'q1_num' => $num1,
            'q2_num' => $num2,
            'q3_num' => $num3,
            'q4_num' => $num4,
            'q5_num' => $num5,
            'total_num' => $num1 + $num2 + $num3 + $num4 + $num5,
            'answer' => [],
        ];
    }
}